package com.koron.css2.systemManage.servlet;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.serviceManage.bean.PostBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.serviceManage.vo.BusinessAreaVo;
import com.koron.css2.systemManage.bean.OrgUserBean;
import com.koron.css2.systemManage.bean.OrgUserQueryBean;
import com.koron.css2.systemManage.bean.RoleBean;
import com.koron.css2.systemManage.mapper.OrgUserMapper;
import com.koron.css2.systemManage.mapper.PostUserMapper;
import com.koron.css2.systemManage.service.IRoleService;
import com.koron.css2.systemManage.service.ServiceFactory;
import com.koron.util.Constant;
import com.koron.util.DBSourceUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 获取当前水司用户列表
 * @author 59532
 *
 */
public class OrgUserList implements ServerInterface {
	
	private static Logger log = LogManager.getLogger(OrgUserList.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);
		try {
			String orgCode = userInfo.getCurWaterCode();
			IRoleService service = ServiceFactory.getRoleService();
			OrgUserQueryBean bean = JsonUtils.objectToPojo(req.getData(), OrgUserQueryBean.class);
			if(StringUtils.isNotEmpty(bean.getCompanyNo())) {
				orgCode = bean.getCompanyNo(); 
			} 
			OrgUserMapper mapper = factory.getMapper(OrgUserMapper.class, DBSourceUtils.getDbEnv(orgCode));
			PageHelper.startPage(bean.getPage(), bean.getPageCount());
			List<OrgUserBean> list = mapper.selectList(bean);
			//获取缴费方式数据字典
			BusinessAreaMapper areaMapper = factory.getMapper(BusinessAreaMapper.class, "_default");
			if(Objects.equals(Constant.APP_CUSTOMER, userInfo.getCurService())) {
				PostUserMapper postUserMapper = factory.getMapper(PostUserMapper.class);
				list.stream().forEach(t -> {
					List<PostBean> post = postUserMapper.selectPostByAccount(t.getAccount());
					if(post != null) {
						String postName = post.stream().map(e -> e.getPostName()).collect(Collectors.joining(","));
						String postNo = post.stream().map(e -> e.getPostNo()).collect(Collectors.joining(","));
						t.setPostName(postName);
						t.setPostNo(postNo);
					}
					if(StringUtils.isNotBlank(t.getBusinessArea())) {
						BusinessAreaVo businessArea = areaMapper.selectByCode(t.getBusinessArea());
						t.setBusinessAreaName(businessArea == null ? "" : businessArea.getAreaName());
					}
					List<RoleBean> roles = service.listRoleByUser(t, ApplicationConfig.getAppCustomerAppid());
					if(roles != null) {
						String roleName = roles.stream().map(e -> e.getRoleName()).collect(Collectors.joining(","));
						String roleCode = roles.stream().map(e -> e.getRoleCode()).collect(Collectors.joining(","));
						t.setRoleName(roleName);
						t.setRoleCode(roleCode);
					}
				});
			}
			info.setData(new PageInfo<>(list));
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			log.error(e.getMessage(), e);
		}
		return info;
	}
}
